package com.adinnet.admin.content.controller;


import com.adinnet.admin.classs.model.vo.ClassResp;
import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.content.model.Content;
import com.adinnet.admin.content.model.vo.AdvertisementReq;
import com.adinnet.admin.content.service.ContentService;
import com.adinnet.admin.system.model.SysRole;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 素材表 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-14
 */
@Controller
@RequestMapping("admin/content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private CommonService commonService;

    //@RequiresPermissions("advertisement:view")
    @RequestMapping("advertisement")
    public String advertisement() {
        return "content/advertisement.html";
    }
    //关于公司页面
    @RequestMapping("/company")
    public String company(Model model) {
        model.addAttribute("companyDetails",contentService.getOne(new QueryWrapper<Content>().eq("deleted", 0).eq("type", 2).last("limit 1")));//校区列表
        return "company/company.html";
    }

    //招聘信息
    @RequestMapping("/jobOffers")
    public String jobOffers(Model model) {
        model.addAttribute("jobDetails",contentService.getOne(new QueryWrapper<Content>().eq("deleted", 0).eq("type", 5).last("limit 1")));//校区列表
        return "jobOffers/jobOffers.html";
    }


    //豆藤的养成
    @RequestMapping("/peaseInfo")
    public String peaseInfo(Model model) {
        model.addAttribute("peaseInfo",contentService.getOne(new QueryWrapper<Content>().eq("deleted", 0).eq("type", 1).last("limit 1")));//校区列表
        return "peaseInfo/peaseInfo.html";
    }

    /**
     * 查询所有广告
     **/
    //@RequiresPermissions("advertisement:view")
    @ResponseBody
    @RequestMapping("advertisement/list")
    public PageResult<Content> list(AdvertisementReq req) {
        //SysUserInfo sysUserInfo=commonService.getLoginUser();
        return contentService.advertisementList(req);
    }

    /**
     * 添加广告
     **/
    //@RequiresPermissions("advertisement:update")
    @ResponseBody
    @RequestMapping("advertisement/add")
    public JsonResult add(Content ad,HttpServletRequest request) {
        if (contentService.save(ad)) {
            commonService.saveOperation(request,"广告弹窗管理-新增广告,广告id为："+ad.getId());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    /**
     * 修改广告
     **/
    //@RequiresPermissions("advertisement:update")
    @ResponseBody
    @RequestMapping("advertisement/update")
    public JsonResult update(Content ad,HttpServletRequest request) {
        if (contentService.updateById(ad)) {
            commonService.saveOperation(request,"广告弹窗管理-编辑广告,广告id为："+ad.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }

    /**
     * 修改广告状态
     **/
    @ResponseBody
    @RequestMapping("advertisement/updateState")
    public JsonResult updateState(Integer adId, Integer state, HttpServletRequest request) {
        if (adId == null) {
            return JsonResult.error("参数adId不能为空");
        }
        if (state == null || (state != 0 && state != 1)) {
            return JsonResult.error("状态值不正确");
        }
        if(state==1){
            Content c = contentService.getOne(new QueryWrapper<Content>().eq("deleted", 0).eq("status", state)
                    .in("type",3,4).last("Limit 1"));
            if(c!=null){
                return JsonResult.error("修改失败,只能启用一条广告");
            }
        }
        Content content = new Content();
        content.setId(adId);
        content.setStatus(state);
        if (contentService.updateById(content)) {
            commonService.saveOperation(request,"广告弹窗管理-修改广告状态");
            return JsonResult.ok();
        } else {
            return JsonResult.error();
        }
    }


    /**
     * 删除广告
     **/
    //@RequiresPermissions("advertisement:update")
    @ResponseBody
    @RequestMapping("advertisement/delete")
    public JsonResult delete(Integer adId) {
        if (contentService.removeById(adId)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    /**
     * 批量删除广告
     */
    @RequestMapping("advertisement/deleteIds")
    @ResponseBody
    public JsonResult deleteIds(String adIds, HttpServletRequest request) {
        commonService.saveOperation(request,"广告弹窗管理-删除广告：删除的广告id为："+adIds);
        return contentService.deleteIds(adIds);
    }

    /**
     * 查询关于公司/招聘信息/豆藤的养成 详情
     **/
    @ResponseBody
    @RequestMapping("/details")
    public JsonResult details(Integer type) {
        return contentService.details(type);
    }

    /**
     * 添加关于公司/招聘信息/豆藤的养成
     **/
    @ResponseBody
    @RequestMapping("/add")
    public JsonResult addContent(Content content,HttpServletRequest request) {
        if (contentService.save(content)) {
            commonService.saveOperation(request,"新增素材：新增的素材id为："+content.getId()+"类型为："+content.getType());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    /**
     * 修改关于公司/招聘信息/豆藤的养成
     **/
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult updateContent(Content content,HttpServletRequest request) {
        Content content1 = contentService.getById(content.getId());
        String contentTypeName="";
        if(content1.getType()!=null){
            switch (content1.getType()){
                case 1:
                    contentTypeName="豆藤的养成";
                    break;
                case 2:
                    contentTypeName="关于公司";
                    break;
                case 5:
                    contentTypeName="招聘信息";
                    break;
                default:
                    break;
            }
        }
        if (contentService.updateById(content)) {
            commonService.saveOperation(request,"更新"+contentTypeName+"：更新的"+contentTypeName+"id为："+content.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }
}
